package com.eis.util;

import java.io.StringReader;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.xml.transform.StringResult;

public class XmlUtil {
	private Jaxb2Marshaller jaxb2Mashaller;
	
	public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
		this.jaxb2Mashaller = jaxb2Mashaller;
	}
	
	public Object parseXml(String xml) {
		if (xml != null) {
			Source source = new StreamSource(new StringReader(xml));
			return jaxb2Mashaller.unmarshal(source);
		} else {
			return null;
		}
	}
	
	public String toXml(Object obj) {
		if (obj != null) {
			StringResult result = new StringResult();
			jaxb2Mashaller.marshal(obj, result);
			return result.toString();
		} else {
			return null;
		}
	}
}
